﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Kalficom.Forms
{
    public partial class frmGeographicZone_CreateModify : Form
    {
        public frmGeographicZone_CreateModify()
        {
            InitializeComponent();
            conf.Icon(this);
        }

        Libraies.db db = new Libraies.db();
        Query.qry_zone query = new Query.qry_zone();
        Libraies.msg msg = new Libraies.msg();

        private void btnOk_Click(object sender, EventArgs e)
        {
            // Check Identifiation blank field
            if (txtCode.Text.Trim() == "")
            {
                MessageBox.Show(msg.zone_codeBlankMsg, msg.captionMsg, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                txtCode.Focus();
                return;
            }

            DataRow row = tblZone.NewRow();
            row["zone_code"] = txtCode.Text;
            row["zone_label"] = txtLabel.Text;
            row["user_create"] = conf.CurUser;
            row["user_date"] = db.GetDate();

            // If form's Tag is null -> create new zone.
            // If from's Tag not null -> modify zone. (Tag is zone_id for modify)
            if (this.Tag == null)
            {

                // Check Identifiation dublicate field.
                if (db.ExecuteScalar(string.Format(query.scalar, db.SqlString(txtCode.Text))) != "")
                {
                    MessageBox.Show(msg.zone_codeExistMsg, msg.captionMsg, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    txtCode.Focus();
                    return;
                }

                if (MessageBox.Show(msg.saveMsg, msg.captionMsg, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    // Add zone into database.
                    tblZone.Rows.Add(row);

                    db.CommandBuilder(string.Format(query.get_where, 0), tblZone);

                    // Close the form and return dialog result OK.
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
            }
            else
            {
                // Check Identifiation dublicate field.
                string zone_id = db.ExecuteScalar(string.Format(query.scalar, db.SqlString(txtCode.Text)));
                if (zone_id != "" && zone_id != tblZone.Rows[0]["zone_id"].ToString())
                {
                    MessageBox.Show(msg.zone_codeExistMsg, msg.captionMsg, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    txtCode.Focus();
                    return;
                }

                if (MessageBox.Show(msg.saveMsg, msg.captionMsg, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    // Update zone into database.
                    row["zone_id"] = this.Tag;
                    tblZone.Rows[0].ItemArray = row.ItemArray;

                    db.CommandBuilder(string.Format(query.get_where, this.Tag), tblZone);

                    // Close the form and return dialog result OK.
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }

            }
        }

        private void frmGeographicZone_CreateModify_Load(object sender, EventArgs e)
        {
            if (this.Tag != null)
            {
                db.FillDataSet(string.Format(query.get_where, this.Tag), tblZone);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
